package com.xc.controller;


import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.ContentCachingRequestWrapper;

import com.ag.utils.RequestUtils;
import com.alibaba.fastjson.JSONObject;
import com.xc.stock.utils.Utils;

import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@RestController
@RequestMapping({"/api/market/"})
public class MarketProxyApi {
	public static void main(String[] args) {
		String string = HttpUtil.get("http://43.132.230.51:8081/stock-market/api/market/stock/gethotgubalist.do");
		System.out.println(string);
	}
	
	@RequestMapping("/**")
	public JSONObject index(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String method = request.getMethod();
		String marketUri = StringUtils.replace(request.getRequestURI(), request.getContextPath(), "");
		log.info("目标marketUri:{}", marketUri);
		marketUri = StringUtils.replace(marketUri, "/api/market/", "/api/hq/");
		log.info("代理marketUri:{}", marketUri);
//		Map<String, String[]> map = request.getParameterMap();
		String queryString = request.getQueryString();
		String body = null;
		if(request instanceof ContentCachingRequestWrapper) {
			body = RequestUtils.getPostParm(request);
		}
		
		return Utils.proxy(marketUri, method, queryString, body, Utils.marketServerList());
	}
}
